﻿/*  -------------------------------------------------
    This file is part of the SchoolTube Live Platform
    Copyright © 2009 SchoolTube, LLC
    
    Licensed under the GNU General Public License v3
    http://www.gnu.org/licenses/gpl.html
    
    http://code.google.com/p/schooltubelive/
    ------------------------------------------------- */

var authURL = "FILL IN AUTH URL HERE";

var stLV = new LoadVars();
var stReturn = new LoadVars();
var now = new Date();

application.onAppStart = function()
{
	application.nextId = 1;
	application.pendingConnections = new Object();
	application.instanceId = application.name.split('/');
	application.allowDebug = true;
}

application.onConnect = function( pClient, p_autoSenseBW, pUsername, pPassword ) {

	if ((pClient.agent.indexOf("FME") != -1) || (pClient.agent.indexOf("FMLE") != -1) || (pClient.agent.indexOf("FMSc") != -1)) {
		stLogin = pClient.uri.split('?')[1];
		if (stLogin != undefined && stLogin.indexOf("&") != -1 && stLogin.indexOf("username=") != -1 && stLogin.indexOf("password=") != -1) {
			pUsername = stLogin.split('&')[0].replace('username=', '');
			pPassword = stLogin.split('&')[1].replace('password=', '');
			pClient.agentType = "FME";
		} else {
			trace(now.toUTCString() + ': Rejected FME - IP:' + pClient.ip);
			this.rejectConnection(pClient);
			return;
		}
	}

	pClient.writeAccess = "";
		if (pUsername != undefined && pPassword != undefined) {
			if (pClient.agentType == undefined) {
				pClient.agentType = "SWF";
			}
			pClient.username = pUsername;
			pClient.writeAccess = pUsername.toLowerCase() + ';appSO/' + pUsername.toLowerCase();
			pClientId = application.nextId++;
			application.pendingConnections[pClientId] = pClient; 
			stLV.pClientId = pClientId;
			stLV.pUsername = pUsername;
			stLV.pPassword = pPassword;
			stLV.sendAndLoad(authURL, stReturn);
		} else {
			trace(now.toUTCString() + ': Accepted viewer - IP:' + pClient.ip);
			this.acceptConnection(pClient);
			if (p_autoSenseBW) {
				pClient.checkBandwidth();
			} else {
				pClient.call("onBWDone");
			}
		}
}

application.onPublish = function(clientObj, streamObj) {
	clientObj.currentStream = streamObj.name.toLowerCase();
	var shObj = SharedObject.get('appSO/' + clientObj.currentStream, true);
	if (shObj.getProperty("totalViewerCount") < 1) {
		shObj.setProperty("totalViewerCount", 0);
	}
	if (shObj.getProperty("totalConnectCount") < 1) {
		shObj.setProperty("totalConnectCount", 0);
	}
	if (shObj.getProperty("streamName") == undefined) {
		shObj.setProperty("streamName", clientObj.currentStream);
	}
	shObj.setProperty("isLive", 1);
	//shObj.setProperty("totalConnectCount", 0);
}

application.onUnpublish = function(clientObj, streamObj) {
	var shObj = SharedObject.get('appSO/' + clientObj.currentStream, true);
	shObj.setProperty("isLive", 0);
}

application.onDisconnect = function (clientObj) {
	if (clientObj.currentStream != undefined) {
		var shObj = SharedObject.get('appSO/' + clientObj.currentStream.toLowerCase(), true);
		viewCount = shObj.getProperty("totalViewerCount");
		if (viewCount > 0) {
			shObj.setProperty("totalViewerCount", (viewCount - 1));
		}
	}
}

stReturn.onData = function () {
	if (stReturn.loginStatus == 1) {
		application.acceptConnection(application.pendingConnections[stReturn.clientId]);
		trace(now.toUTCString() + ': Accepted broadcast - ID:' + stReturn.clientId
			+ ', Type:' + application.pendingConnections[stReturn.clientId].agentType
			+ ', Username:' + application.pendingConnections[stReturn.clientId].username
			+ ', IP:' + application.pendingConnections[stReturn.clientId].ip);
	} else {
		application.rejectConnection(application.pendingConnections[stReturn.clientId]);
		trace(now.toUTCString() + ': Rejected broadcast - ID:' + stReturn.clientId
			+ ', Type:' + application.pendingConnections[stReturn.clientId].agentType
			+ ', Username:' + application.pendingConnections[stReturn.clientId].username
			+ ', IP:' + application.pendingConnections[stReturn.clientId].ip);
	}
}

Client.prototype.getStreamLength = function(p_streamName) {return 0;}

Client.prototype.setStreamName = function(streamName, newName) {
	soName = 'appSO/' + streamName.toLowerCase();
	if (this.writeAccess.indexOf(soName) != -1) {
		var shObj = SharedObject.get('appSO/' + streamName.toLowerCase(), true);
		shObj.setProperty("streamName", newName);
	}
}

Client.prototype.addViewCount = function(p_streamName) {
		var shObj = SharedObject.get('appSO/' + p_streamName.toLowerCase(), true);
		viewCount = shObj.getProperty("totalViewerCount");
		shObj.setProperty("totalViewerCount", (viewCount + 1));
		connectCount = shObj.getProperty("totalConnectCount");
		shObj.setProperty("totalConnectCount", (connectCount + 1));
		this.currentStream = p_streamName.toLowerCase();
}


Client.prototype.FCPublish = function( streamname ) {
	s = Stream.get(streamname);
	var err = false;
	if (this.username.toLowerCase() != streamname.toLowerCase()) {
		err = true;
	}
	if (!err) {
		this.call("onFCPublish", null, {code:"NetStream.Publish.Success", description:streamname});
	} else {
		trace(now.toUTCString() + ': FME Error: Disconnecting Client - Invalid stream name: ' + streamname);
		this.call("onFCPublish", null, {code:"NetStream.Publish.BadName", description:streamname});
		s.record(false);
		s.flush();
		application.disconnect(this);
	}	
}

Client.prototype.FCUnpublish = function( streamname ) {
    this.call("onFCUnpublish", null, {code:"NetStream.Unpublish.Success", description:streamname});
}

Client.prototype.releaseStream = function(streamname){}

Client.prototype.DVRSetStreamInfo = function(DVRInfo){}